/**
 * 报表设计公司(2011)
 */
package net.abbp.report.designer.action.file;

import java.awt.Window;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JFileChooser;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileNameExtensionFilter;

import net.abbp.core.util.StringUtils;
import net.abbp.report.context.ReportContext;
import net.abbp.report.export.PdfExporter;
import net.abbp.report.model.ReportModel;
import net.abbp.report.view.GridView;
import net.abbp.swing.util.ImageUtils;

import com.reportgear.designer.action.ReportAction;
import com.reportgear.report.model.calculator.CalculatorHelper;
import com.reportgear.report.print.ReportPage;
import com.reportgear.report.print.ReportPageIterator;

/**
 * PDF导出
 * 
 * @version 1.0 2011-3-24
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since ANNCSR 2.0
 */
public class ExportPDFAction extends ReportAction {

    public ExportPDFAction() {
        setName("PDF导出");
        setSmallIcon(ImageUtils.readIcon("file/print.png"));
    }

    public void actionPerformed(ActionEvent e) {
        JFileChooser fc = new JFileChooser();
        fc.setFileFilter(new FileNameExtensionFilter("PDF文件", "pdf"));
        fc.setDialogTitle("导出为PDF文件");
        GridView gridView = ReportContext.INSTANCE.getCurrentView();
        Window wnd = SwingUtilities.getWindowAncestor(gridView);
        if (fc.showSaveDialog(wnd) == JFileChooser.APPROVE_OPTION) {
            String fileName = fc.getSelectedFile().getAbsolutePath();

            if (!fileName.toLowerCase().endsWith(".pdf")) {
                fileName = fileName + ".pdf";
            }

            ReportModel report = gridView.getReportModel();

            ReportModel currReport = CalculatorHelper.calResumeModel(report, StringUtils.convertToString(
                    ReportContext.INSTANCE.getBusItem("PersonId"), "0"));

            List<ReportPage> pages = new ArrayList<ReportPage>();
            Iterator<ReportPage> rs = new ReportPageIterator(report);
            while (rs.hasNext()) {
                pages.add(rs.next());
            }

            new PdfExporter().exportPDF(pages, fileName);
        }
    }
}
